package sfmc.biz.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import sfmc.biz.service.SfmcCustomerService;
import sfmc.biz.service.SfmcHolidayService;
import sfmc.biz.vo.Sfmc_TB_CUSTOMER;
import sfmc.biz.vo.Sfmc_TB_HOLIDAY;
import sfmc.biz.vo.Sfmc_TB_ORDER;
import sfmc.common.util.DateUtil;

@Controller
public class SfmcHolidayController {
	
	protected Log log = LogFactory.getLog(this.getClass());
	

	@Autowired 
	private SfmcHolidayService sfmcHolidayService;
	
	@RequestMapping(value ="/holiday_list.do")
	public ModelAndView list(ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_HOLIDAY sfmc_TB_HOLIDAY){
		
		ModelAndView mv = new ModelAndView("/holiday/holiday_list");

		if("".equals(sfmc_TB_HOLIDAY.getSearchFromDate())){
			sfmc_TB_HOLIDAY.setSearchFromDate(DateUtil.getCurrentTime("yyyy")+"0101");
			sfmc_TB_HOLIDAY.setSearchToDate(DateUtil.getCurrentTime("yyyy")+"1231");
		}
		
		sfmc_TB_HOLIDAY.setSearchFromDate(sfmc_TB_HOLIDAY.getSearchFromDate().replaceAll("-", ""));
		sfmc_TB_HOLIDAY.setSearchToDate(sfmc_TB_HOLIDAY.getSearchToDate().replaceAll("-", ""));
		
		List<Sfmc_TB_HOLIDAY> holidayList = sfmcHolidayService.getHolidayList(sfmc_TB_HOLIDAY);
		
		GsonBuilder gsonbuilder = new GsonBuilder();
		Gson gson = gsonbuilder.create();
		gson = gsonbuilder.setPrettyPrinting().create();
		mv.addObject("holidaylist", gson.toJson(holidayList));
		mv.addObject("holidaydata", sfmc_TB_HOLIDAY);
		
		return mv;
	}
	
	@RequestMapping(value ="/holiday_save.do" , method = RequestMethod.POST)
	public ModelAndView save(ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_HOLIDAY sfmc_TB_HOLIDAY){
		
		ModelAndView mv = new ModelAndView("/holiday/holiday_result");

		int returnVal = -1;
		String type = "";

		sfmc_TB_HOLIDAY.setDataHoliDate(sfmc_TB_HOLIDAY.getDataHoliDate().replaceAll("-", ""));
		
		if(sfmc_TB_HOLIDAY.getDataHoliIdx() == 0){
			returnVal = sfmcHolidayService.insertHoliday(sfmc_TB_HOLIDAY);
			type = "C";
		}else{
			returnVal = sfmcHolidayService.updateHoliday(sfmc_TB_HOLIDAY);
			type = "U";
		}
		
		mv.addObject("holidaydata", sfmc_TB_HOLIDAY);
		mv.addObject("returnVal", returnVal);
		mv.addObject("type", type);
		return mv;
	}
	
	@RequestMapping(value ="/holiday_delete.do" , method = RequestMethod.POST)
	public ModelAndView delete(ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_HOLIDAY sfmc_TB_HOLIDAY){
		
		ModelAndView mv = new ModelAndView("/holiday/holiday_result");

		int returnVal = -1;
		String type = "D";
		
		if(sfmc_TB_HOLIDAY.getDataHoliIdx() != 0){
			returnVal = sfmcHolidayService.deleteHoliday(sfmc_TB_HOLIDAY);
		}
		
		mv.addObject("holidaydata", sfmc_TB_HOLIDAY);
		mv.addObject("returnVal", returnVal);
		mv.addObject("type", type);		
		return mv;
	}
	
	@RequestMapping(value="/holiday_excel.do")
public void listsToExcel(HttpServletResponse resp, OutputStream ost,ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_HOLIDAY sfmc_TB_HOLIDAY) throws Exception{
		
		FileOutputStream fos = null;
		
		List<Sfmc_TB_HOLIDAY> excelList = sfmcHolidayService.getHolidayList(sfmc_TB_HOLIDAY);
		resp.setHeader("Content-Disposition", "attachment; filename=holidayList.xls");
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet firstSheet  = workbook.createSheet("sheet1");
		
		for(int i=0; i<excelList.size(); i++){
			
			HSSFRow rowHeader = firstSheet.createRow(0);
			HSSFCell headerA = rowHeader.createCell(0);
			HSSFCell headerB = rowHeader.createCell(1);
			headerA.setCellValue(new HSSFRichTextString("휴일날자"));
			headerB.setCellValue(new HSSFRichTextString("휴일명"));
			
			sfmc_TB_HOLIDAY = excelList.get(i);
			HSSFRow rowA = firstSheet.createRow(i);
			HSSFCell cells1 = rowA.createCell(0); 
			HSSFCell cells2 = rowA.createCell(1);
			cells1.setCellValue(sfmc_TB_HOLIDAY.getHoliDate());
			cells2.setCellValue(sfmc_TB_HOLIDAY.getBigo());	
		}
		
		 try{
	        	workbook.write(ost);
	        	ost.flush();
	        	fos = new FileOutputStream(new File("C:/holidayList.xls"));
	        	workbook.write(fos);
	        }
	        catch(Exception e){
	        	log.fatal(e.getMessage()+"\n");
	        	log.fatal(e.getStackTrace());
	        }
	        finally{
	        	if(fos != null){
	        		try{
		        		fos.flush();
		                fos.close();
	        		}
	        		catch(IOException e){
	        			log.fatal(e.getMessage());
	        		}
	        	}
	        }
		
	}//end of listsToExcel 
	


}


